package com.oplink.webems.modules.alarm.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.oplink.webems.common.persistence.Page;
import com.oplink.webems.common.snmp.entity.SccCard;
import com.oplink.webems.common.snmp.service.SnmpService;
import com.oplink.webems.modules.alarm.entity.AlarmCurrent;
import com.oplink.webems.modules.alarm.entity.AlarmProp;
import com.oplink.webems.modules.alarm.entity.AlarmSeverity;
import com.oplink.webems.modules.alarm.entity.AlarmType;
import com.oplink.webems.modules.alarm.service.AlarmCurrentService;
import com.oplink.webems.modules.alarm.service.AlarmHistoryService;
import com.oplink.webems.modules.alarm.service.AlarmPropService;
import com.oplink.webems.modules.cfg.entity.CfgNe;
import com.oplink.webems.modules.cfg.entity.CfgNeType;
import com.oplink.webems.modules.cfg.entity.CfgSite;
import com.oplink.webems.modules.cfg.entity.CfgSlot;
import com.oplink.webems.modules.cfg.services.CfgNeService;
import com.oplink.webems.modules.cfg.services.CfgNetypeService;
import com.oplink.webems.modules.cfg.services.CfgSeverityService;
import com.oplink.webems.modules.cfg.services.CfgSiteService;
import com.oplink.webems.modules.cfg.services.CfgSlotService;
import com.oplink.webems.modules.sys.service.ShiroDbRealm.ShiroUser;
import com.oplink.webems.modules.sys.web.BaseController;

/**
 * Current Alarm Controller
 * @author yizhang
 */
@Controller
@RequestMapping(value = "alarm/alarmcur")
public class AlarmCurrentController extends BaseController {

	@Autowired
	private AlarmCurrentService alarmCurrentService;

	@Autowired
	private AlarmHistoryService alarmHistService;
	
	
	@Autowired
	private AlarmPropService alarmPropService;

	@Autowired
	private CfgSiteService siteservice;

	@Autowired
	private CfgSiteService siteService;

	@Autowired
	private CfgNeService neService;

	@Autowired
	private CfgSeverityService severityService;

	@Autowired
	private CfgNetypeService netypeService;

	@Autowired
	private CfgSlotService slotService;

	@Autowired
	private SnmpService snmpService;

	@ModelAttribute
	public AlarmCurrent get(@RequestParam(required = false) Long id) {
		if (id != null) {
			return alarmCurrentService.getAlarmCurrent(id);
		} else {
			return new AlarmCurrent();
		}
	}

	@RequestMapping(value = { "list", "" })
	public String list(AlarmCurrent alarmcurrent, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<CfgSite> slist = siteService.getAllSite();
		List<CfgNe> nelist = neService.getAllNe();
		List<AlarmSeverity> selist = severityService.getAllSeverity();
		List<AlarmType> atlist = alarmPropService.getAllAlarmType();
		List<CfgNeType> netypelist = netypeService.getAllNeType();
		List<CfgSlot> slotlist = slotService.getAllSlot();
		Page<AlarmCurrent> pages = alarmCurrentService.find(new Page<AlarmCurrent>(request, response), alarmcurrent);
		model.addAttribute("slist", slist);
		model.addAttribute("nelist", nelist);
		model.addAttribute("selist", selist);
		model.addAttribute("slotlist", slotlist);
		model.addAttribute("atlist", atlist);
		model.addAttribute("netypelist", netypelist);
		model.addAttribute("pages", pages);
		model.addAttribute("acur", alarmcurrent);
		return "modules/alarm/alarm_cur";
	}
	
	/**
	 * 当前告警同步
	 * @param alarmcurrent
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "cursync")
	public String cursync(AlarmCurrent alarmcurrent, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		String ip = "172.16.166.231";
		if (alarmcurrent!=null){
			if (alarmcurrent.getNenode()!=null){
			    CfgNe node = neService.getOneCfgNe(alarmcurrent.getNenode().getNid());
					ip = node.getIp();
			}
		}
		SccCard cardTemp = new SccCard();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		//snmpService = new SnmpService("172.16.166.231", "Admin@123");
		snmpService = new SnmpService(ip, "Admin@123");
		List<CfgSite> slist = siteService.getAllSite();
		List<CfgNe> nelist = neService.getAllNe();
		List<AlarmSeverity> selist = severityService.getAllSeverity();
		List<AlarmType> atlist = alarmPropService.getAllAlarmType();
		List<CfgNeType> netypelist = netypeService.getAllNeType();
		List<CfgSlot> slotlist = slotService.getAllSlot();
		try {
			String alarm = snmpService.get(cardTemp, "GetCurrentAlarm");
			//String alarm = "AlarmCnt=14,Slot1:POWERB_ABNORMAL:2014-10-6 16:38:21,Slot1:DCP_BUT_FORCE_TO_MAIN:2014-10-6 15:52:52,Slot1:DCP_BACK_SW_DAMAGE:2014-10-6 15:52:52,Slot1:DCP_BACK_RXA(P1)_LOS:2014-10-6 15:52:52,Slot1:DCP_MAIN_SW_DAMAGE:2014-10-6 15:52:52,Slot1:DCP_MAIN_RXC(P1')_LOS:2014-10-6 15:52:52,Slot1:POWERB_REMOVE:2014-10-6 16:25:31,Slot2:SLOT1_REMOVE:2014-10-6 16:25:32,Slot3:SLOT2_REMOVE:2014-10-6 16:25:32,Slot4:SLOT3_REMOVE:2014-10-6 16:25:33,Slot5:SLOT4_REMOVE:2014-10-6 16:25:35,Slot5:TEMP_LOW:2014-10-6 16:25:34,Slot6:TEMP_HIGH:2014-10-6 16:52:7,Slot7:FAN_ABNORMAL:2014-10-6 16:52:7";
			if (alarm != null) {
				String[] aArr = alarm.split(",");
				//String[] aArr = alarm.split(" ");
				String[] barr = null;
				String slotStr = "", alarmname = "", splitTemp = "", otime = "";
				Map<String, String> map = new HashMap<String, String>();
				for (int i = 1; i < aArr.length; i++) {
					map = new HashMap<String, String>();
					barr = aArr[i].split(":");
					slotStr = barr[0];
					alarmname = barr[1];
					splitTemp = slotStr + ":" + alarmname + ":";
					otime = aArr[i].replace(splitTemp, "").trim();
					map.put("slot", slotStr.replace("Slot", "").trim());
					map.put("aname", alarmname);
					map.put("otime", otime);
					map.put("neip", snmpService.getIp());
					list.add(map);
				}
			    alarmCurrentService.synAlarmSave(list);
			}			
			Page<AlarmCurrent> pages = alarmCurrentService.find(new Page<AlarmCurrent>(request, response), alarmcurrent);
			model.addAttribute("slist", slist);
			model.addAttribute("nelist", nelist);
			model.addAttribute("selist", selist);
			model.addAttribute("slotlist", slotlist);
			model.addAttribute("atlist", atlist);
			model.addAttribute("netypelist", netypelist);
			model.addAttribute("pages", pages);
			model.addAttribute("acur", alarmcurrent);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "modules/alarm/alarm_cur";
	}

	/**
	 * 单条当前告警信息Json形式
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "open/{id}", method = RequestMethod.GET)
	@ResponseBody
	public String oneCurAlarm(@PathVariable("id") Long id) {
        AlarmCurrent acur = alarmCurrentService.getAlarmCurrent(id);
        String json = alarmCurrentService.acurToJson(acur);
		return json;
	}	
	
	@RequestMapping(value = "confalarm/{ids}")
	@ResponseBody
	@Transactional
	public String confalarm(@PathVariable("ids") String ids, RedirectAttributes redirectAttributes) {
		try {
			System.out.println("ids ids ids >> "+ids);
			String ret = "0";
			if (!"".equals(ids)) {
				String[] idarr = ids.split(",");
				Long uid = getCurrentUserId();
				alarmHistService.saveAlarmHist(idarr, uid);
				alarmCurrentService.delCurRow1(idarr);
				ret = "1";
			} else {
				ret = "0";
			}
			return ret;
		} catch (Exception e) {
			return "0";
		}
	}	
	
//	public Long[] convertionToLong(String[] strs) {
//		Long[] longs = new Long[strs.length];
//		for (int i = 0; i < strs.length; i++) {
//			String str = strs[i];
//			long thelong = Long.valueOf(str);
//			longs[i] = thelong;
//		}
//		return longs;
//	}
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
